//
//  HomePageCollectionViewController.m
//  product_B
//
//  Created by lanou on 16/5/30.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "HomePageCollectionViewController.h"
#import "HomePageMainModel.h"
#import "HomePageCollectionViewCell.h"
#import "HomePageLayout.h"

@interface HomePageCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)HomePageLayout *homePageLayout;
@end

@implementation HomePageCollectionViewController

- (instancetype)init{
    if (self = [super init]) {
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake((kWidth - 50) / 2-25, 25, 100, 30)];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.dataArray = [[NSMutableArray alloc]init];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.titleLabel.text = self.dateString;
    self.searchButton.frame = CGRectMake(20, 30, 80, 20);
    [self.searchButton setTitle:@"往期列表" forState:UIControlStateNormal];
    [self.searchButton setTintColor:[UIColor grayColor]];
    [self.searchButton setBackgroundImage:nil forState:UIControlStateNormal];
    [self.musicButton removeFromSuperview];
    [self.userButton removeFromSuperview];

    [self netRequest];
    self.homePageLayout = [[HomePageLayout alloc]init];
    self.homePageLayout.width = (kWidth - 32) /2;
    self.homePageLayout.numberOfColum = 2;
    self.homePageLayout.space = 12;
    self.homePageLayout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 0);
 
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight-64) collectionViewLayout:self.homePageLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"HomePageCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    
 
}

- (void)netRequest{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:[NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/hp/bymonth/%@",self.dateString] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *dataArray = dataDic[@"data"];
        for (NSDictionary *dic in dataArray) {
            HomePageMainModel *model = [[HomePageMainModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        
        __block HomePageCollectionViewController *blockself = self;
        self.homePageLayout.resultBlock = ^(NSInteger i){
            HomePageMainModel *model = blockself.dataArray[i];
            CGSize size = [model.hp_content boundingRectWithSize:CGSizeMake(170, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size;
            return size.height+180;
        };
        [self.collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomePageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    HomePageMainModel *model = self.dataArray[indexPath.row];
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:model.hp_img_original_url]];
    cell.titleLabel.text = model.hp_title;
    cell.contentLabel.text = model.hp_content;
    return cell;
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    return CGSizeMake(167, 200);
//}
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//    return UIEdgeInsetsMake(13, 13, 13, 13);
//}
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
//    return 10;
//}
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
//    return 10;
//}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)searchButtonAction:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
